Skip to contentSkip to conversation

Change in openvpn[master]: [CMake] Allow unit tests to fall back to hard coded location

Openvpn-devel In review plaisthos (Code Review) 3 participants 6 messages Jan 29, 2024 – Feb 1, 2024 1 message not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): gerrit.1706531144000.Ide72b81f497088dd0fd2cdcfff83cbce5b48f145@...2715...

#1 lore raw

Attention is currently required from: flichtenheld.

Hello flichtenheld,

I'd like you to do a code review.
Please visit

    http://gerrit.openvpn.net/c/openvpn/+/509?usp=email

to review the following change.

Change subject: [CMake] Allow unit tests to fall back to hard coded location
......................................................................

[CMake] Allow unit tests to fall back to hard coded location

Settings the environment variable required for running unit tests is tiresome in my IDE (Clion). So allow unit tests to fall back to a hard coded location in case the environment variable is not set.

Change-Id: Ide72b81f497088dd0fd2cdcfff83cbce5b48f145
---
M CMakeLists.txt
M tests/unit_tests/openvpn/test_common.h
M tests/unit_tests/openvpn/test_user_pass.c

3 files changed, 37 insertions(+), 6 deletions(-)

git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/09/509/1

CMakeLists.txt +4 -0
index be55c60..fdd2b01 100644
@@ -682,6 +682,10 @@
 
         target_include_directories(${test_name} PRIVATE src/openvpn)
 
+        # for compat with IDEs like Clion that ignore the tests properties
+        # for the environment variable srcdir when running tests as fallback
+        target_compile_definitions(${test_name} PRIVATE "-DUNIT_TEST_SOURCEDIR=\"${CMAKE_SOURCE_DIR}/tests/unit_tests/openvpn\"")
+
         if (NOT ${test_name} STREQUAL "test_buffer")
             target_sources(${test_name} PRIVATE
                 src/openvpn/buffer.c
tests/unit_tests/openvpn/test_common.h +28 -1
index 50e16d6..f219e93 100644
@@ -32,9 +32,36 @@
  * This has a openvpn prefix to avoid confusion with cmocka's unit_test_setup_*
  * methods
  */
-void
+static inline void
 openvpn_unit_test_setup()
 {
     assert_int_equal(setvbuf(stdout, NULL, _IONBF, BUFSIZ), 0);
     assert_int_equal(setvbuf(stderr, NULL, _IONBF, BUFSIZ), 0);
 }
+
+/**
+ * Helper function to get a file path from the unit test directory to open it
+ * or pass its path to another function. This function will first look for
+ * an environment variable or if failing that, will fall back to a hardcoded
+ * value from compile time if compiled with CMake.
+ *
+ * @param buf           buffer holding the path to the file
+ * @param bufsize       size of buf
+ * @param filename      name of the filename to retrieve relative to the
+ *                      unit test source directory
+ */
+void
+openvpn_test_get_srcdir_dir(char *buf, size_t bufsize, const char *filename)
+{
+    const char *srcdir = getenv("srcdir");
+
+#if defined(UNIT_TEST_SOURCEDIR)
+    if (!srcdir)
+    {
+        srcdir = UNIT_TEST_SOURCEDIR;
+    }
+#endif
+    assert_non_null(srcdir);
+
+    snprintf(buf, bufsize, "%s/%s", srcdir, filename);
+}
tests/unit_tests/openvpn/test_user_pass.c +5 -5
index ab4dfe4..fee5891 100644
@@ -35,6 +35,7 @@
 #include <string.h>
 #include <setjmp.h>
 #include <cmocka.h>
+#include "test_common.h"
 
 #include "misc.c"
 
@@ -209,11 +210,9 @@
     reset_user_pass(&up);
     unsigned int flags = 0;
 
-    const char *srcdir = getenv("srcdir");
-    assert_non_null(srcdir);
     char authfile[PATH_MAX] = { 0 };
+    openvpn_test_get_srcdir_dir(authfile, PATH_MAX, "input/user_pass.txt" );
 
-    snprintf(authfile, PATH_MAX, "%s/%s", srcdir, "input/user_pass.txt");
     /*FIXME: query_user_exec() called even though nothing queued */
     will_return(query_user_exec_builtin, true);
     assert_true(get_user_pass_cr(&up, authfile, "UT", flags, NULL));
@@ -223,7 +222,7 @@
 
     reset_user_pass(&up);
 
-    snprintf(authfile, PATH_MAX, "%s/%s", srcdir, "input/user_only.txt");
+    openvpn_test_get_srcdir_dir(authfile, PATH_MAX, "input/user_only.txt");
     expect_string(query_user_exec_builtin, query_user[i].prompt, "Enter UT Password:");
     will_return(query_user_exec_builtin, "cpassword");
     will_return(query_user_exec_builtin, true);
@@ -236,7 +235,7 @@
     reset_user_pass(&up);
 
     flags |= GET_USER_PASS_PASSWORD_ONLY;
-    snprintf(authfile, PATH_MAX, "%s/%s", srcdir, "input/user_only.txt");
+    openvpn_test_get_srcdir_dir(authfile, PATH_MAX, "input/user_only.txt");
     /*FIXME: query_user_exec() called even though nothing queued */
     will_return(query_user_exec_builtin, true);
     assert_true(get_user_pass_cr(&up, authfile, "UT", flags, NULL));
@@ -256,5 +255,6 @@
 int
 main(void)
 {
+    openvpn_unit_test_setup();
     return cmocka_run_group_tests(user_pass_tests, NULL, NULL);
 }

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/509?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Ide72b81f497088dd0fd2cdcfff83cbce5b48f145
Gerrit-Change-Number: 509
Gerrit-PatchSet: 1
Gerrit-Owner: plaisthos <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: flichtenheld <[email protected]...>
Gerrit-MessageType: newchange

#2 lore raw
In reply to a message not in the archive

Attention is currently required from: plaisthos.

flichtenheld has posted comments on this change. ( http://gerrit.openvpn.net/c/openvpn/+/509?usp=email )

Change subject: [CMake] Allow unit tests to fall back to hard coded location
......................................................................

Patch Set 1: Code-Review+2

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/509?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Ide72b81f497088dd0fd2cdcfff83cbce5b48f145
Gerrit-Change-Number: 509
Gerrit-PatchSet: 1
Gerrit-Owner: plaisthos <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Comment-Date: Thu, 01 Feb 2024 11:41:07 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
Gerrit-MessageType: comment

#3 lore raw

PATCH 1/1 Allow unit tests to fall back to hard coded location

In reply to a message not in the archive

From: Arne Schwabe <[email protected]...>

Settings the environment variable required for running unit tests is tiresome in my IDE (Clion). So allow unit tests to fall back to a hard coded location in case the environment variable is not set.

Change-Id Ide72b81f497088dd0fd2cdcfff83cbce5b48f145

---

This change was reviewed on Gerrit and approved by at least one developer. I request to merge it to master.

Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/509
This mail reflects revision 1 of this Change. Acked-by according to Gerrit (reflected above):
Frank Lichtenheld <[email protected]...>

CMakeLists.txt +4 -0
index be55c60..fdd2b01 100644
@@ -682,6 +682,10 @@
 
         target_include_directories(${test_name} PRIVATE src/openvpn)
 
+        # for compat with IDEs like Clion that ignore the tests properties
+        # for the environment variable srcdir when running tests as fallback
+        target_compile_definitions(${test_name} PRIVATE "-DUNIT_TEST_SOURCEDIR=\"${CMAKE_SOURCE_DIR}/tests/unit_tests/openvpn\"")
+
         if (NOT ${test_name} STREQUAL "test_buffer")
             target_sources(${test_name} PRIVATE
                 src/openvpn/buffer.c
tests/unit_tests/openvpn/test_common.h +28 -1
index 50e16d6..f219e93 100644
@@ -32,9 +32,36 @@
  * This has a openvpn prefix to avoid confusion with cmocka's unit_test_setup_*
  * methods
  */
-void
+static inline void
 openvpn_unit_test_setup()
 {
     assert_int_equal(setvbuf(stdout, NULL, _IONBF, BUFSIZ), 0);
     assert_int_equal(setvbuf(stderr, NULL, _IONBF, BUFSIZ), 0);
 }
+
+/**
+ * Helper function to get a file path from the unit test directory to open it
+ * or pass its path to another function. This function will first look for
+ * an environment variable or if failing that, will fall back to a hardcoded
+ * value from compile time if compiled with CMake.
+ *
+ * @param buf           buffer holding the path to the file
+ * @param bufsize       size of buf
+ * @param filename      name of the filename to retrieve relative to the
+ *                      unit test source directory
+ */
+void
+openvpn_test_get_srcdir_dir(char *buf, size_t bufsize, const char *filename)
+{
+    const char *srcdir = getenv("srcdir");
+
+#if defined(UNIT_TEST_SOURCEDIR)
+    if (!srcdir)
+    {
+        srcdir = UNIT_TEST_SOURCEDIR;
+    }
+#endif
+    assert_non_null(srcdir);
+
+    snprintf(buf, bufsize, "%s/%s", srcdir, filename);
+}
tests/unit_tests/openvpn/test_user_pass.c +5 -5
index ab4dfe4..fee5891 100644
@@ -35,6 +35,7 @@
 #include <string.h>
 #include <setjmp.h>
 #include <cmocka.h>
+#include "test_common.h"
 
 #include "misc.c"
 
@@ -209,11 +210,9 @@
     reset_user_pass(&up);
     unsigned int flags = 0;
 
-    const char *srcdir = getenv("srcdir");
-    assert_non_null(srcdir);
     char authfile[PATH_MAX] = { 0 };
+    openvpn_test_get_srcdir_dir(authfile, PATH_MAX, "input/user_pass.txt" );
 
-    snprintf(authfile, PATH_MAX, "%s/%s", srcdir, "input/user_pass.txt");
     /*FIXME: query_user_exec() called even though nothing queued */
     will_return(query_user_exec_builtin, true);
     assert_true(get_user_pass_cr(&up, authfile, "UT", flags, NULL));
@@ -223,7 +222,7 @@
 
     reset_user_pass(&up);
 
-    snprintf(authfile, PATH_MAX, "%s/%s", srcdir, "input/user_only.txt");
+    openvpn_test_get_srcdir_dir(authfile, PATH_MAX, "input/user_only.txt");
     expect_string(query_user_exec_builtin, query_user[i].prompt, "Enter UT Password:");
     will_return(query_user_exec_builtin, "cpassword");
     will_return(query_user_exec_builtin, true);
@@ -236,7 +235,7 @@
     reset_user_pass(&up);
 
     flags |= GET_USER_PASS_PASSWORD_ONLY;
-    snprintf(authfile, PATH_MAX, "%s/%s", srcdir, "input/user_only.txt");
+    openvpn_test_get_srcdir_dir(authfile, PATH_MAX, "input/user_only.txt");
     /*FIXME: query_user_exec() called even though nothing queued */
     will_return(query_user_exec_builtin, true);
     assert_true(get_user_pass_cr(&up, authfile, "UT", flags, NULL));
@@ -256,5 +255,6 @@
 int
 main(void)
 {
+    openvpn_unit_test_setup();
     return cmocka_run_group_tests(user_pass_tests, NULL, NULL);
 }
#4 lore raw

[Openvpn-devel] [PATCH applied] Re: Allow unit tests to fall back to hard coded location

Tested locally (freebsd, autoconf) and on the GHA builds.

Your patch has been applied to the master branch.

commit bb0849db20747dc4acea394c4db807a47cbc526b

Author: Arne Schwabe
Date:   Thu Feb 1 15:48:17 2024 +0100
     Allow unit tests to fall back to hard coded location

     Acked-by: Frank Lichtenheld <[email protected]...>
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/[email protected]/msg28161.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering

#5 lore raw

[Openvpn-devel] [S] Change in openvpn[master]: Allow unit tests to fall back to hard coded location

In reply to a message not in the archive

cron2 has uploaded a new patch set (#2) to the change originally created by plaisthos. ( http://gerrit.openvpn.net/c/openvpn/+/509?usp=email )

The following approvals got outdated and were removed:
Code-Review+2 by flichtenheld

Change subject: Allow unit tests to fall back to hard coded location
......................................................................

Allow unit tests to fall back to hard coded location

Settings the environment variable required for running unit tests is tiresome in my IDE (Clion). So allow unit tests to fall back to a hard coded location in case the environment variable is not set.

Change-Id Ide72b81f497088dd0fd2cdcfff83cbce5b48f145

Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg28161.html

---
M CMakeLists.txt
M tests/unit_tests/openvpn/test_common.h
M tests/unit_tests/openvpn/test_user_pass.c

3 files changed, 37 insertions(+), 6 deletions(-)

git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/09/509/2

CMakeLists.txt +4 -0
index be55c60..fdd2b01 100644
@@ -682,6 +682,10 @@
 
         target_include_directories(${test_name} PRIVATE src/openvpn)
 
+        # for compat with IDEs like Clion that ignore the tests properties
+        # for the environment variable srcdir when running tests as fallback
+        target_compile_definitions(${test_name} PRIVATE "-DUNIT_TEST_SOURCEDIR=\"${CMAKE_SOURCE_DIR}/tests/unit_tests/openvpn\"")
+
         if (NOT ${test_name} STREQUAL "test_buffer")
             target_sources(${test_name} PRIVATE
                 src/openvpn/buffer.c
tests/unit_tests/openvpn/test_common.h +28 -1
index 50e16d6..f219e93 100644
@@ -32,9 +32,36 @@
  * This has a openvpn prefix to avoid confusion with cmocka's unit_test_setup_*
  * methods
  */
-void
+static inline void
 openvpn_unit_test_setup()
 {
     assert_int_equal(setvbuf(stdout, NULL, _IONBF, BUFSIZ), 0);
     assert_int_equal(setvbuf(stderr, NULL, _IONBF, BUFSIZ), 0);
 }
+
+/**
+ * Helper function to get a file path from the unit test directory to open it
+ * or pass its path to another function. This function will first look for
+ * an environment variable or if failing that, will fall back to a hardcoded
+ * value from compile time if compiled with CMake.
+ *
+ * @param buf           buffer holding the path to the file
+ * @param bufsize       size of buf
+ * @param filename      name of the filename to retrieve relative to the
+ *                      unit test source directory
+ */
+void
+openvpn_test_get_srcdir_dir(char *buf, size_t bufsize, const char *filename)
+{
+    const char *srcdir = getenv("srcdir");
+
+#if defined(UNIT_TEST_SOURCEDIR)
+    if (!srcdir)
+    {
+        srcdir = UNIT_TEST_SOURCEDIR;
+    }
+#endif
+    assert_non_null(srcdir);
+
+    snprintf(buf, bufsize, "%s/%s", srcdir, filename);
+}
tests/unit_tests/openvpn/test_user_pass.c +5 -5
index 277cb1d..bd4eb1f 100644
@@ -35,6 +35,7 @@
 #include <string.h>
 #include <setjmp.h>
 #include <cmocka.h>
+#include "test_common.h"
 
 #include "misc.c"
 
@@ -232,11 +233,9 @@
     reset_user_pass(&up);
     unsigned int flags = 0;
 
-    const char *srcdir = getenv("srcdir");
-    assert_non_null(srcdir);
     char authfile[PATH_MAX] = { 0 };
+    openvpn_test_get_srcdir_dir(authfile, PATH_MAX, "input/user_pass.txt" );
 
-    snprintf(authfile, PATH_MAX, "%s/%s", srcdir, "input/user_pass.txt");
     /*FIXME: query_user_exec() called even though nothing queued */
     will_return(query_user_exec_builtin, true);
     assert_true(get_user_pass_cr(&up, authfile, "UT", flags, NULL));
@@ -246,7 +245,7 @@
 
     reset_user_pass(&up);
 
-    snprintf(authfile, PATH_MAX, "%s/%s", srcdir, "input/user_only.txt");
+    openvpn_test_get_srcdir_dir(authfile, PATH_MAX, "input/user_only.txt");
     expect_string(query_user_exec_builtin, query_user[i].prompt, "Enter UT Password:");
     will_return(query_user_exec_builtin, "cpassword");
     will_return(query_user_exec_builtin, true);
@@ -259,7 +258,7 @@
     reset_user_pass(&up);
 
     flags |= GET_USER_PASS_PASSWORD_ONLY;
-    snprintf(authfile, PATH_MAX, "%s/%s", srcdir, "input/user_only.txt");
+    openvpn_test_get_srcdir_dir(authfile, PATH_MAX, "input/user_only.txt");
     /*FIXME: query_user_exec() called even though nothing queued */
     will_return(query_user_exec_builtin, true);
     assert_true(get_user_pass_cr(&up, authfile, "UT", flags, NULL));
@@ -279,5 +278,6 @@
 int
 main(void)
 {
+    openvpn_unit_test_setup();
     return cmocka_run_group_tests(user_pass_tests, NULL, NULL);
 }

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/509?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Ide72b81f497088dd0fd2cdcfff83cbce5b48f145
Gerrit-Change-Number: 509
Gerrit-PatchSet: 2
Gerrit-Owner: plaisthos <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-MessageType: newpatchset

#6 lore raw

[Openvpn-devel] [S] Change in openvpn[master]: Allow unit tests to fall back to hard coded location

In reply to a message not in the archive

cron2 has submitted this change. ( http://gerrit.openvpn.net/c/openvpn/+/509?usp=email )

Change subject: Allow unit tests to fall back to hard coded location
......................................................................

Allow unit tests to fall back to hard coded location

Settings the environment variable required for running unit tests is tiresome in my IDE (Clion). So allow unit tests to fall back to a hard coded location in case the environment variable is not set.

Change-Id Ide72b81f497088dd0fd2cdcfff83cbce5b48f145

Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg28161.html

---
M CMakeLists.txt
M tests/unit_tests/openvpn/test_common.h
M tests/unit_tests/openvpn/test_user_pass.c

3 files changed, 37 insertions(+), 6 deletions(-)
CMakeLists.txt +4 -0
index be55c60..fdd2b01 100644
@@ -682,6 +682,10 @@
 
         target_include_directories(${test_name} PRIVATE src/openvpn)
 
+        # for compat with IDEs like Clion that ignore the tests properties
+        # for the environment variable srcdir when running tests as fallback
+        target_compile_definitions(${test_name} PRIVATE "-DUNIT_TEST_SOURCEDIR=\"${CMAKE_SOURCE_DIR}/tests/unit_tests/openvpn\"")
+
         if (NOT ${test_name} STREQUAL "test_buffer")
             target_sources(${test_name} PRIVATE
                 src/openvpn/buffer.c
tests/unit_tests/openvpn/test_common.h +28 -1
index 50e16d6..f219e93 100644
@@ -32,9 +32,36 @@
  * This has a openvpn prefix to avoid confusion with cmocka's unit_test_setup_*
  * methods
  */
-void
+static inline void
 openvpn_unit_test_setup()
 {
     assert_int_equal(setvbuf(stdout, NULL, _IONBF, BUFSIZ), 0);
     assert_int_equal(setvbuf(stderr, NULL, _IONBF, BUFSIZ), 0);
 }
+
+/**
+ * Helper function to get a file path from the unit test directory to open it
+ * or pass its path to another function. This function will first look for
+ * an environment variable or if failing that, will fall back to a hardcoded
+ * value from compile time if compiled with CMake.
+ *
+ * @param buf           buffer holding the path to the file
+ * @param bufsize       size of buf
+ * @param filename      name of the filename to retrieve relative to the
+ *                      unit test source directory
+ */
+void
+openvpn_test_get_srcdir_dir(char *buf, size_t bufsize, const char *filename)
+{
+    const char *srcdir = getenv("srcdir");
+
+#if defined(UNIT_TEST_SOURCEDIR)
+    if (!srcdir)
+    {
+        srcdir = UNIT_TEST_SOURCEDIR;
+    }
+#endif
+    assert_non_null(srcdir);
+
+    snprintf(buf, bufsize, "%s/%s", srcdir, filename);
+}
tests/unit_tests/openvpn/test_user_pass.c +5 -5
index 277cb1d..bd4eb1f 100644
@@ -35,6 +35,7 @@
 #include <string.h>
 #include <setjmp.h>
 #include <cmocka.h>
+#include "test_common.h"
 
 #include "misc.c"
 
@@ -232,11 +233,9 @@
     reset_user_pass(&up);
     unsigned int flags = 0;
 
-    const char *srcdir = getenv("srcdir");
-    assert_non_null(srcdir);
     char authfile[PATH_MAX] = { 0 };
+    openvpn_test_get_srcdir_dir(authfile, PATH_MAX, "input/user_pass.txt" );
 
-    snprintf(authfile, PATH_MAX, "%s/%s", srcdir, "input/user_pass.txt");
     /*FIXME: query_user_exec() called even though nothing queued */
     will_return(query_user_exec_builtin, true);
     assert_true(get_user_pass_cr(&up, authfile, "UT", flags, NULL));
@@ -246,7 +245,7 @@
 
     reset_user_pass(&up);
 
-    snprintf(authfile, PATH_MAX, "%s/%s", srcdir, "input/user_only.txt");
+    openvpn_test_get_srcdir_dir(authfile, PATH_MAX, "input/user_only.txt");
     expect_string(query_user_exec_builtin, query_user[i].prompt, "Enter UT Password:");
     will_return(query_user_exec_builtin, "cpassword");
     will_return(query_user_exec_builtin, true);
@@ -259,7 +258,7 @@
     reset_user_pass(&up);
 
     flags |= GET_USER_PASS_PASSWORD_ONLY;
-    snprintf(authfile, PATH_MAX, "%s/%s", srcdir, "input/user_only.txt");
+    openvpn_test_get_srcdir_dir(authfile, PATH_MAX, "input/user_only.txt");
     /*FIXME: query_user_exec() called even though nothing queued */
     will_return(query_user_exec_builtin, true);
     assert_true(get_user_pass_cr(&up, authfile, "UT", flags, NULL));
@@ -279,5 +278,6 @@
 int
 main(void)
 {
+    openvpn_unit_test_setup();
     return cmocka_run_group_tests(user_pass_tests, NULL, NULL);
 }

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/509?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Ide72b81f497088dd0fd2cdcfff83cbce5b48f145
Gerrit-Change-Number: 509
Gerrit-PatchSet: 2
Gerrit-Owner: plaisthos <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-MessageType: merged